/**
 * 
 */
package com.tjyx.maintain.action;

import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.servlet.ServletRequestContext;

import com.tjyx.common.TJYXException;
import com.tjyx.common.action.BaseAction;
import com.tjyx.common.util.DTOUtil;
import com.tjyx.common.util.UploadFilePic;
import com.tjyx.common.util.constants.TableNameConstants;
import com.tjyx.maintain.bo.NewsBo;
import com.tjyx.maintain.dao.NewDao;
import com.tjyx.maintain.dao.NewsClassDao;
import com.tjyx.maintain.dto.News;
import com.tjyx.maintain.dto.NewsClass;
import com.tjyx.maintain.dto.UserInfo;

/**
 * 新闻管理的action
 * 
 * @author liuyuxin
 * 
 */
public class NewsAction extends BaseAction {
	public static NewsClassDao newsClassDao = new NewsClassDao();
	public static NewDao newDao = new NewDao();
	public static NewsBo newsBo = new NewsBo();

	public String addNews(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<NewsClass> newsclasss;
		try {
			newsclasss = newsClassDao.queryNewsClass();
			request.setAttribute("allNewsClass", newsclasss);
		} catch (SQLException e) {
			System.out.println("发布新闻时，查询所有的新闻分类失败");
			e.printStackTrace();
		}
		RequestContext requestContext = new ServletRequestContext(request);
	   if(FileUpload.isMultipartContent(requestContext)){
		   uploadNews(request,response);
	   }
		return "maintain/news/buildUpNews.jsp";
	}

	
	/**
	 * 更新新闻信息信息
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public String updateNewsById(HttpServletRequest request,
			HttpServletResponse response)throws IOException {
		String idString=request.getParameter("id");
		if(idString!=null&!idString.equals("")){
			int id=Integer.valueOf(idString);
			System.out.println("修改的id为==="+id);
			News news=new News();
			try {
				news = newDao.queryById(id);
				request.setAttribute("news", news);
			} catch (SQLException e) {
				System.out.println("更新新闻失败！");
				e.printStackTrace();
			}
		}
		return "maintain/news/buildUpNews.jsp";
	}

	/**
	 * 查询新闻信息列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String managerNewsList(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		System.out.println("dsdadfdfsa");
		initPagination(request);
		String json;
		try {
			json = newsBo.getPaginationJSON(page, rows, News.class,
					TableNameConstants.MAINTAIN_NewsInfo);
			write2View(json, response);
		} catch (TJYXException e) {
			e.printStackTrace();
			write2View(e.getMessage(), response);
		}
		return NO_DISPATCH;
	}
	
	
/**
 * 添加新闻（包括上传图片）
 * @param request
 * @param response
 */
 public static void  uploadNews(HttpServletRequest request,
			HttpServletResponse response){
		HashMap param_hm = UploadFilePic.begin_upload(request,
				response, 1);// 1表示上传的是新闻图片
		if(param_hm==null){
			return;
		}
		String newsClassId="0";
		String newsTitle="";
		String newsPic="";
		String newsContent="";
		String hitNumber="";
		int hit=0;
		String createPerson="";
		Date addTime=new Date();
		 java.sql.Date   dd;
		String isOrign="";
		if (param_hm.get("newsClassSelect") != null&&!param_hm.get("newsClassSelect").equals("")) {
			newsClassId = param_hm.get("newsClassSelect").toString();
		}
		if (param_hm.get("newsTitle") != null&&!param_hm.get("newsTitle").equals("")) {
			newsTitle = param_hm.get("newsTitle").toString();
		}
		if (param_hm.get("filename") != null&&!param_hm.get("filename").equals("")) {
			newsPic = param_hm.get("filename").toString();
		}
		if (param_hm.get("elm1") != null&&!param_hm.get("elm1") .equals("")) {
			newsContent = param_hm.get("elm1").toString();
		}
		if (param_hm.get("hitNumber") != null&&!param_hm.get("hitNumber").equals("")) {
			hitNumber = param_hm.get("hitNumber").toString();
			hit=Integer.valueOf(hitNumber);
		}
		if (param_hm.get("createPerson") != null&&!param_hm.get("createPerson") .equals("")) {
			createPerson = param_hm.get("createPerson").toString();
		}else{
			createPerson="本站编辑员";
		}	
		if (param_hm.get("addTime") != null&&!param_hm.get("addTime").equals("")) {
		 String	addTimeString = param_hm.get("addTime").toString();
			if (addTimeString == null||addTimeString.equals("")) {
				addTime = new Date();
			}else{
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				 try {
					addTime = df.parse(addTimeString);
				} catch (ParseException e) {
					System.out.println("新闻添加时，添加时间异常！");
					e.printStackTrace();
				}
			}
		        dd    =    new    java.sql.Date(addTime.getTime()); 
		}else{
			dd    =    new    java.sql.Date(addTime.getTime()); 
		}
		if (param_hm.get("isOrign") != null&&!param_hm.get("isOrign").equals("")) {
			isOrign = param_hm.get("isOrign").toString();
		}else{
			isOrign="本站";
		}
		try {
			newDao.addNews(newsTitle, newsContent, Integer
					.valueOf(newsClassId), hit, createPerson, dd,
					isOrign,newsPic);
			request.setAttribute("addNewsMessage","添加成功！");
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
 }
 
}
